package com.qiche.aspect;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.qiche.common.ResponseCode;
import com.qiche.enums.ResultEnum;

import java.io.Serializable;

/**
 * http请求返回的最外层对象
 *
 * @author Administrator
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化Json的时候，如果value是null的对象,key也会消失
public class Result<T> implements Serializable{

    /**
     * 错误码
     */
    private int code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 具体的内容，携带数据实体
     */
    private T data;

    private Result() {
    }


    private Result(int code){
        this.code=code;
    }

    private Result(int code,T data){
        this.code=code;
        this.data=data;
    }

    private Result(int code,String msg,T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }

    private Result(int code,String msg){
        this.code=code;
        this.msg=msg;
    }

    @JsonIgnore
    public boolean isSuccess(){
        return  this.code == ResponseCode.SUCCESS.getCode();
    }
    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> Result<T> createBySuccess(){
        return  new Result<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T> Result<T> createBySuccessMessage(String msg){
        return  new Result<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
    public static <T> Result<T> createBySuccess(T data){
        return  new Result<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> Result<T> createBySuccess(String msg,T data){
        return  new Result<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> Result<T> createByError(){
        return  new Result<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public static <T> Result<T> createByErrorMessage(String errorMsg){
        return  new Result<T>(ResponseCode.ERROR.getCode(),errorMsg);
    }
    
    public static <T> Result<T> createByError(String msg,T data){
        return  new Result<T>(ResponseCode.ERROR.getCode(),msg,data);
    }

    public static <T> Result<T> createByErrorCodeMessage(int errorCode,String errorMsg){
        return  new Result<T>(errorCode,errorMsg);
    }

    public static <T> Result<T> createByResultEnum(ResultEnum resultEnum){
        return new Result<T>(resultEnum.getCode(),resultEnum.getMsg());
    }

    public static <T> Result<T> createByResultEnum(ResultEnum resultEnum,T data){
        return new Result<T>(resultEnum.getCode(),resultEnum.getMsg(),data);
    }




}
